<?php
namespace app\zhihui\controller;

use app\common\model\ZhMonks as ZhMonksModel;
use app\common\model\ZhCate as ZhCateModel;
use app\home\controller\Base;

/**
 * 高僧大德控制器
 * Class Zhmonks
 * @package app\index\controller
 */
class Zhmonks extends Base
{
    /**
     * [index  列表]
     * @return json
     * @author [C。]
     */
    public function index()
    {
        $param = input('param.');
        $model = new ZhMonksModel;
        $cmodel = new ZhCateModel;
        $this->getPageAndSize($param);
        $map = [];
        try {
            $map['states'] = 1;
            $map['comid'] = $param['comid'];
            $count = $model->getCount($map);//满足条件的总数
            $allpage = intval(ceil($count / $this->size));//总页数
            $lists = $model->getlist($map, $this->from, $this->size, 'id,headimg,fahao,cate');
            foreach($lists as &$v){
                if($v['cate'] == 5){
                    $v['cname'] = '现任住持';
                }elseif($v['cate'] == 6){
                    $v['cname'] = '历代高僧';
                }else{
                    $data = $cmodel->findData($v['cate'],'id','cname');
                    $v['cname'] = $data['cname'];
                }
            }
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功', [
            'count' => $count,//总记录数
            'allpage' => $allpage,  //总页数
            'lists' => $lists,  //列表数据
        ]);
    }
    /**
     * [get  获取一条]
     * @return json
     * @author [C。]
     */
    public function get()
    {
        $param = input('param.');
        $model = new ZhMonksModel;
        try{
            $data = $model->findData($param['id'],'id','headimg,fahao,cate,content');
        } catch (\Exception $e) {
            systembug($e->getMessage(),2,$this->url);
            return admin_json(-1, $e->getMessage());
        }
        return admin_json(1, '获取成功',$data);
    }
}